//
//  ViewController.m
//  Test
//
//  Created by 周启睿 on 2020/6/9.
//  Copyright © 2020 周启睿. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"

@interface ViewController ()
@property (strong, nonatomic) UITextView *textView;
@property (strong, nonatomic) UIButton *imageBtn;
@property (strong, nonatomic) UIButton *triangleBtn;
@property (strong, nonatomic) UIButton *sortBtn;
@property (strong, nonatomic) UIButton *randomBtn;

@end

@implementation ViewController
# pragma mark -
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    CGSize size = [[UIScreen mainScreen] bounds].size;
    UITextView* textView = [[UITextView alloc] init];
    textView.textAlignment = NSTextAlignmentCenter;
    textView.editable = false;
    textView.font = [UIFont systemFontOfSize:16];
    _textView = textView;
    
    UIButton* sortBtn = [[UIButton alloc] init];
    [sortBtn addTarget:self action:@selector(onSortClick) forControlEvents:UIControlEventTouchUpInside];
    [sortBtn setTitle:@"成绩排序" forState:UIControlStateNormal];
    sortBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    sortBtn.layer.borderWidth = 1;
    _sortBtn = sortBtn;
    
    UIButton* imageBtn = [[UIButton alloc] init];
    [imageBtn addTarget:self action:@selector(onImageClick) forControlEvents:UIControlEventTouchUpInside];
    [imageBtn setTitle:@"尖锥图案" forState:UIControlStateNormal];
    imageBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    imageBtn.layer.borderWidth = 1;
    _imageBtn = imageBtn;
    
    UIButton* triangleBtn = [[UIButton alloc] init];
    [triangleBtn addTarget:self action:@selector(onTriangleClick) forControlEvents:UIControlEventTouchUpInside];
    [triangleBtn setTitle:@"杨辉三角" forState:UIControlStateNormal];
    triangleBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    triangleBtn.layer.borderWidth = 1;
    _triangleBtn = triangleBtn;
    
    UIButton* randomBtn = [[UIButton alloc] init];
    [randomBtn addTarget:self action:@selector(onCalcTextClick) forControlEvents:UIControlEventTouchUpInside];
    [randomBtn setTitle:@"计算字符" forState:UIControlStateNormal];
    randomBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    randomBtn.layer.borderWidth = 1;
    _randomBtn = randomBtn;
    
    
    [self.view addSubview:self.textView];
    [self.view addSubview:self.sortBtn];
    [self.view addSubview:self.imageBtn];
    [self.view addSubview:self.triangleBtn];
    [self.view addSubview:self.randomBtn];
    
    self.sortBtn.frame = CGRectMake(0, 50 ,size.width / 4, 30);
    
    self.imageBtn.frame = CGRectMake( CGRectGetMaxX(self.sortBtn.frame),
                                        CGRectGetMinY(self.sortBtn.frame),
                                        CGRectGetWidth(self.sortBtn.frame),
                                        CGRectGetHeight(self.sortBtn.frame));
    
    self.triangleBtn.frame = CGRectMake(CGRectGetMaxX(self.imageBtn.frame),
                                        CGRectGetMinY(self.imageBtn.frame),
                                        CGRectGetWidth(self.imageBtn.frame),
                                        CGRectGetHeight(self.imageBtn.frame));
    
    self.randomBtn.frame = CGRectMake(CGRectGetMaxX(self.triangleBtn.frame),
                                           CGRectGetMinY(self.triangleBtn.frame),
                                           CGRectGetWidth(self.triangleBtn.frame),
                                           CGRectGetHeight(self.triangleBtn.frame));
    
    self.textView.frame = CGRectMake(0, CGRectGetMaxY(self.sortBtn.frame) + 10, size.width, size.height / 3 * 2);
}

- (void)onTriangleClick{
    //杨辉三角
    
    NSString* rule = @"生成如图所示杨辉三角，深度不限.";
    
    
    int n = 10;
    int i,j=0;
    int num,nun;
    int  a[20]={0,1};
    NSString* text = @"";
    for (i=1; i<=n; i++) {
        num=0;
        for (j=1; j<=i; j++) {
            nun=a[j];
            a[j]=num+nun;
            num=nun;
            text = [text stringByAppendingFormat:@"%3d",a[j]];
        }
        text = [text stringByAppendingString:@"\n"];
    }
    self.textView.text = [NSString stringWithFormat:@"%@\n\n%@",rule,text];
}

- (void)onImageClick{
    //菱形
    NSString* text = @"";
    NSInteger maxNum = 10;
    for (NSInteger i = 0; i < maxNum; i ++) {
        for (NSInteger k = 0 ; k < i ; k++) {
            text = [text stringByAppendingFormat:@"%3ld",i];
        }
        text = [text stringByAppendingString:@"\n"];
    }
    for (NSInteger i = maxNum; i > 0; i --) {
        for (NSInteger k = 0 ; k < i ; k++) {
            text = [text stringByAppendingFormat:@"%3ld",i];
        }
        text = [text stringByAppendingString:@"\n"];
    }
    self.textView.text = text;
}

- (void)onSortClick{
    
    NSString* rule = @"生成任意n个学生的成绩，将这些学生按成绩排序.高分同学排在前面,分数相同时排名一致,下一个低分数同学排名为总人数中的排名.\n备注:不能使用oc数组中排序api\n重要:需要实现排序和排名";
    
    //成绩排序
    NSMutableArray<Student*>* ary = [NSMutableArray array];
    for (NSInteger i = 0; i < 15; i++) {
        [ary addObject:[Student ModelWithName:[NSString stringWithFormat:@"姓名%ld",i] grade:arc4random()%20 + 60]];
    }
    
    NSString *org = [NSString stringWithFormat:@"原成绩(乱序):\n%@",[ary componentsJoinedByString:@",\n"]];
   
    //冒泡
    NSArray<Student*> *sortedArray = [ary sortedArrayUsingComparator:^NSComparisonResult(Student *p1, Student *p2){
        return p1.grade < p2.grade;
    }];
    
    NSMutableArray* tarAry = [NSMutableArray array];
    __block NSInteger index = 1;
    [sortedArray enumerateObjectsUsingBlock:^(Student* obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx > 0) {
            NSInteger cur = obj.grade;
            NSInteger before = [sortedArray objectAtIndex:idx - 1].grade;
            if (cur != before) {
                index = idx + 1;
            }
        }
        [tarAry addObject:[NSString stringWithFormat:@"%@(排名%ld)",obj,index]];
    }];
    NSString* tar = [NSString stringWithFormat:@"%@\n\n %@\n\n 成绩排名:\n%@",rule,org,[tarAry componentsJoinedByString:@",\n"]];
    self.textView.text = tar;
}

- (void)onCalcTextClick{
    self.textView.textAlignment = NSTextAlignmentLeft;
   
    
    NSString* text = [self randomString];
    
    NSMutableDictionary* res_dict = [NSMutableDictionary dictionary];
    NSMutableArray* ary = [NSMutableArray array];
    for (NSInteger i = 0; i < text.length ; i++) {
        [ary addObject:[text substringWithRange:NSMakeRange(i, 1)]];
    }
    
    NSInteger min = 99;
    for (NSString* key in ary) {
        NSNumber* num = [res_dict objectForKey:key];
        NSInteger index = num.integerValue + 1;
        [res_dict setValue:@(index) forKey:key];
        if (index < min) {
            min = index;
        }
    }
    
    NSString* minText = @"";
    for (NSString* key in ary) {
        NSNumber* num = [res_dict objectForKey:key];
        if (num.integerValue == 1 && key.length > 0) {
            minText = [minText stringByAppendingString:key];
            minText = [minText stringByAppendingString:@","];
        }
    }
    
    NSInteger res_index = 0;
    NSString* res_key = 0;
    for (NSInteger i = ary.count - 1; i > 0; i--) {
        NSString* key = [ary objectAtIndex:i];
        if ([minText containsString:key]) {
            res_index = i;
            res_key = key;
            break;
        }
    }
    
    NSString* rule = @"给定一个字符串(随机字符串生成函数已提供[self randomString],并且会出现重复字符).";
    rule = [rule stringByAppendingString:@"\n\n"];
    rule = [rule stringByAppendingString:@"问题:"];
    rule = [rule stringByAppendingString:@"\n\n"];
    rule = [rule stringByAppendingString:@" 1. 找出出现次数最小的字符.输出字符及其出现次数."];
    rule = [rule stringByAppendingString:@"\n"];
    rule = [rule stringByAppendingFormat:@" 例子: “%@” 字符%@ 各出现%d次。",text,minText,1];
    rule = [rule stringByAppendingString:@"\n\n"];
    rule = [rule stringByAppendingString:@" 2. 找出出现次数最小的字符,并且在最靠后的字符.输出其值及位置."];
    rule = [rule stringByAppendingString:@"\n"];
    rule = [rule stringByAppendingFormat:@" 例子: “%@” 字符是%@ 倒数第%lu位。",text,res_key,ary.count - res_index];
    //NSString* tar = [NSString stringWithFormat:@"%@\n\n %@\n\n 统计字符出现次数:\n%@ \n%@出现最小并且最靠后，位置在倒数第%ld位",rule,text,res_dict,res_key,ary.count - res_index];
    self.textView.text = rule;
    
    
}
# pragma mark -
///随机生成字符串
- (NSString *)randomString
{
    static int kNumber = 10;
    NSString *sourceStr = @"ABCDEFGHIJKLMNOPQRST";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    for (int i = 0; i < kNumber; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
        index = index+1;
        index = index >= sourceStr.length ? index - 1 : index;
        [resultStr appendString:[sourceStr substringWithRange:NSMakeRange(index, 1)]];
    }
    return resultStr;
}
@end
